package com.parking.platform.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    //转换到年月日的
    public static Date stringToDate(String dateString){
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date =format.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    //消除时区的
    public static Date stringForDate(String dateString){
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SSSX");
        Date date = null;
        try {
            date =format.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    //转换到年月日时分秒的
    public static Date string3Date(String dateString){
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date =format.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    //比较时间大小的  date1大于date2就是true
    public static boolean compareDate(Date date1,Date date2){
        long time1 =date1.getTime();
        long time2 =date2.getTime();
        if(time1>time2) return true;
        else return false;
    }
    //年月日时间转换为字符串
    public static String date2String(Date date){
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }
    //年月日时分秒转换为字符串
    public static  String date3StringXS(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static Date convertToLocalDate(Date date) {
        Calendar calendar = Calendar.getInstance(); // 获取当前时区的日历对象
        calendar.setTime(date); // 设置日历对象的时间为输入的日期

        // 清除时、分、秒和毫秒部分
        calendar.clear(Calendar.HOUR_OF_DAY);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);

        return new Date(calendar.getTimeInMillis()); // 返回转换后的本地日期对象
    }


    public static Date convertToLocalDate2(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = sdf.format(date); // 将Date对象格式化成"yyyy-MM-dd"字符串
        java.sql.Date sqlDate = java.sql.Date.valueOf(dateString); // 将格式化后的字符串转换成java.sql.Date对象，会自动去除时分秒和毫秒部分
        return new Date(sqlDate.getTime()); // 再将java.sql.Date对象转换回Date对象并返回
    }

    public static Date removeTimezoneAndTime(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
}
